package liudan.archetype;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * Description:
 * Email: liudan@ucfgroup.com
 * Title
 * Copyright: Copyright (c) 2017
 * User: liudan
 * Created on 2017/4/5 13:37
 */
public class MiniMongoDB {

    /***
     * 更新数据
     * @param path
     * @param tableName
     * @param po
     * @return
     */
    public boolean update(String path, String tableName, Object po) {
        boolean flag = false;
        Gson gson = new GsonBuilder().create();
        FileInputStream fileInputStream = null;
        List<Map> dataResult = new ArrayList<Map>();
        try {
            fileInputStream = new FileInputStream(path);
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(fileInputStream);

            Element root = document.getRootElement();

            List<Element> eroot = root.getChildren();
            for (Element e : eroot) {
                if (e.getAttributeValue("name").equals(tableName)) {
                    List<Element> dataList = e.getChildren();
                    for (Element c : dataList) {
                        String data = c.getText();
                        Map m1 = gson.fromJson(data, Map.class);
                        Map m2 = gson.fromJson(gson.toJson(po), Map.class);
                        if (m1.get("uuid").equals(m2.get("uuid"))) {
                            m1.putAll(m2);
                            Element datas = new Element("data");
                            datas.setText(gson.toJson(m1));
                            e.removeContent(c);
                            e.addContent(datas);
                            break;
                        }
                    }
                }
            }
            XMLOutputter xmlOutputter = new XMLOutputter();
            xmlOutputter.output(document, new FileOutputStream(path));
            flag = true;
            System.out.println("update data success");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;

    }

    /***
     * 删除数据
     * @param path
     * @param tableName
     * @param po
     * @return
     */
    public boolean delete(String path, String tableName, Object po) {
        boolean flag = false;
        Gson gson = new GsonBuilder().create();
        FileInputStream fileInputStream = null;
        List<Map> dataResult = new ArrayList<Map>();
        try {
            fileInputStream = new FileInputStream(path);
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(fileInputStream);

            Element root = document.getRootElement();

            List<Element> eroot = root.getChildren();
            for (Element e : eroot) {
                if (e.getAttributeValue("name").equals(tableName)) {
                    List<Element> dataList = e.getChildren();
                    for (Element c : dataList) {
                        String data = c.getText();
                        Map m1 = gson.fromJson(data, Map.class);
                        Map m2 = gson.fromJson(gson.toJson(po), Map.class);
                        if (m1.get("uuid").equals(m2.get("uuid"))) {
                            e.removeContent(c);
                            break;
                        }
                    }
                }
            }
            XMLOutputter xmlOutputter = new XMLOutputter();
            xmlOutputter.output(document, new FileOutputStream(path));
            flag = true;
            System.out.println("delete data sucdess");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;

    }

    /***
     * 查询数据
     * @param path
     * @param tableName
     * @return
     */
    public List<Map> find(String path, String tableName) {
        boolean flag = false;
        Gson gson = new GsonBuilder().create();
        FileInputStream fileInputStream = null;
        List<Map> dataResult = new ArrayList<Map>();
        try {
            fileInputStream = new FileInputStream(path);
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(fileInputStream);

            Element root = document.getRootElement();

            List<Element> eroot = root.getChildren();
            for (Element e : eroot) {
                if (e.getAttributeValue("name").equals(tableName)) {
                    List<Element> dataList = e.getChildren();
                    for (Element c : dataList) {
                        String data = c.getText();
                        if (data == null) {
                            break;
                        }
                        dataResult.add(gson.fromJson(data, Map.class));
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataResult;

    }

    /***
     * 添加数据
     * @param path
     * @param tableName
     * @param po
     * @return
     * @throws IOException
     * @throws JDOMException
     */
    public boolean insert(String path, String tableName, Object po) {
        boolean flag = false;
        Gson gson = new GsonBuilder().create();
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(fileInputStream);

            Element root = document.getRootElement();

            List<Element> eroot = root.getChildren();
            for (Element e : eroot) {
                if (e.getAttributeValue("name").equals(tableName)) {
                    Map base = new HashMap();
                    base.put("uuid", UUID.randomUUID().toString());
                    Element data = new Element("data");
                    String poJson = gson.toJson(po);
                    Map mpo = gson.fromJson(poJson, Map.class);
                    base.putAll(mpo);
                    data.addContent(gson.toJson(base));
                    e.addContent(data);
                }
            }
            XMLOutputter xmlOutputter = new XMLOutputter();
            xmlOutputter.output(document, new FileOutputStream(path));
            flag = true;
            System.out.println("insert data success");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }


    /***
     * 创建数据库
     * @param path
     */
    public boolean createDataBase(String path, String tableName) {
        boolean flag = false;
        try {
            if (isExist(path, tableName)) {
                return flag;
            }
            FileInputStream fileInputStream = new FileInputStream(path);
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(fileInputStream);
            Element root = document.getRootElement();
            Element tab1 = new Element("table");
            tab1.setAttribute("name", tableName);
            root.addContent(tab1);
            document.setContent(root);
            FileOutputStream fileOutputStream = null;
            fileOutputStream = new FileOutputStream(path);
            XMLOutputter outputter = new XMLOutputter();
            outputter.output(document, fileOutputStream);
            flag = true;
            System.out.println("mongodb database create success!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /***
     * 是否已经存在该集合
     * @param path
     * @param tableName
     * @return
     */
    public boolean isExist(String path, String tableName) {
        boolean flag = false;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            SAXBuilder saxBuilder = new SAXBuilder();
            if (fileInputStream.available() > 0) {
                Document document = saxBuilder.build(fileInputStream);
                Element root = document.getRootElement();
                List<Element> e = root.getChildren();
                //判断数据库中是否已经存在该集合
                for (Element c : e) {
                    if (c.getAttributeValue("name").equals(tableName)) {
                        flag = true;
                        break;
                    }
                }
            } else {
                FileOutputStream fileOutputStream = null;
                Document document = new Document();
                Element root = new Element("database");
                document.setRootElement(root);
                fileOutputStream = new FileOutputStream(path);
                XMLOutputter outputter = new XMLOutputter();
                outputter.output(document, fileOutputStream);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }


}
